package com.example.activity.controller;

import com.example.activity.service.*;
import com.example.activity.so.acvitivi.DeployInfoSO;
import com.example.activity.utils.Result;
import com.example.activity.vo.PageResultVO;
import com.example.activity.vo.acvitivi.DeployInfoVO;
import com.example.activity.vo.upload.FileUpDowmInfoVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 部署
 */
@Controller
@RequestMapping("/deploy")
public class ActivitiDeployController {

    @Resource
    private FileUpLoadService fileUpLoadService;

    @Resource
    private ProcessService processService;

    @Resource
    private DeployInfoService deployInfoService;


    /**
     * 流程文件上传
     *
     * @param fileList
     * @return FileUploadVO
     */
    @PostMapping("/uploadProcess")
    @ResponseBody
    public Result<List<FileUpDowmInfoVO>> uploadProcess(@RequestParam List<MultipartFile> fileList) throws Exception {
        return Result.ok(fileUpLoadService.uploadFile(fileList));
    }

    /**
     * 保存流程信息
     *
     * @param deployInfoVO
     * @return
     */
    @PostMapping("/saveDeployInfo")
    @ResponseBody
    public Result<DeployInfoVO> saveDeployInfo(@RequestBody DeployInfoVO deployInfoVO) {
        return Result.ok(processService.saveDeployInfo(deployInfoVO));
    }

    /**
     * 查询部署的流程信息
     *
     * @param deployInfoSO DeployInfoSO
     * @return List<DeployInfoVO>
     */
    @GetMapping("/deployInfoPageList")
    @ResponseBody
    public Result<PageResultVO<DeployInfoVO>> deployInfoPageList(DeployInfoSO deployInfoSO) {
        return Result.ok(deployInfoService.deployInfoPageList(deployInfoSO));
    }

    /**
     * 部署流程
     *
     * @param deployInfoIdList DeployInfo.id
     * @return
     */
    @PostMapping("/deploy")
    @ResponseBody
    public Result<List<DeployInfoVO>> deploy(@RequestBody List<Long> deployInfoIdList) {
        return Result.ok(processService.deploy(deployInfoIdList));
    }

    /**
     * 删除部署
     *
     * @param deployInfoIdList 主键
     * @return
     */
    @PostMapping("/deleteDeploy")
    @ResponseBody
    public Result<Boolean> deleteDeploy(@RequestBody List<Long> deployInfoIdList) {
        return Result.ok(processService.deleteDeploy(deployInfoIdList));
    }

    @GetMapping("/downloadDeployStream/{fileId}")
    public void downloadDeployStream(@PathVariable Long fileId, HttpServletResponse response) {
        processService.downloadDeployFileStream(fileId, response);
    }

}
